package com.shrct.businessprocess.purchase.impl;

import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.organization.RCSupplier;
import com.shrct.businessprocess.product.RCFullServiceProductEntry;
import com.shrct.businessprocess.product.RCIdentifiablePhysicalProductEntry;
import com.shrct.businessprocess.product.RCProductServiceTracerDAO;
import com.shrct.businessprocess.product.RCServiceLevel;
import com.shrct.businessprocess.product.RCServiceResponseTime;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.purchase.IHasServicePurchase;
import com.shrct.businessprocess.purchase.RCServiceAcceptItem;
import com.shrct.businessprocess.purchase.RCServiceProductPurchaseRequest;
import com.shrct.businessprocess.purchase.RCServicePurchaseOrder;
import com.shrct.businessprocess.purchase.RCServicePurchaseOrderAccept;
import com.shrct.businessprocess.purchase.impl.RCBasePurchaseOrderAcceptImpl;
import com.shrct.businessprocess.purchase.impl.RCServiceAcceptItemImpl;
import com.shrct.businessprocess.purchase.impl.RCServicePurchaseOrderImpl;
import com.shrct.businessprocess.purchase.proxy.RCServicePurchaseOrderAcceptProxy;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("RCSERVICEPURCHASEORDERACCEPT_TYPE")
public class RCServicePurchaseOrderAcceptImpl extends RCBasePurchaseOrderAcceptImpl implements RCServicePurchaseOrderAccept {
  public RCServicePurchaseOrderAcceptImpl() {
    super();
  }
  
  public RCServicePurchaseOrderAcceptImpl(final RCServicePurchaseOrder purchaseOrder) {
    super();
    this.purchaseOrder = purchaseOrder;
  }
  
  @JoinColumn(name = "purchaseOrder_id")
  @ManyToOne(targetEntity = RCServicePurchaseOrderImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCServicePurchaseOrder purchaseOrder;
  
  public RCServicePurchaseOrder getPurchaseOrder() {
    return this.purchaseOrder;
  }
  
  public RCServicePurchaseOrderAccept setPurchaseOrder(final RCServicePurchaseOrder purchaseOrder) {
    this.purchaseOrder = purchaseOrder;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCServiceAcceptItemImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "accept")
  private Collection<RCServiceAcceptItem> acceptItem = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCServiceAcceptItem>();;
  
  public Collection<RCServiceAcceptItem> getAcceptItem() {
    return this.acceptItem;
  }
  
  public RCServiceAcceptItem createAndAddtoAcceptItem() {
    getAcceptItem().size();
    com.shrct.businessprocess.purchase.RCServiceAcceptItem rCServiceAcceptItem = new com.shrct.businessprocess.purchase.impl.RCServiceAcceptItemImpl(this);
    getObjectFactory().create(rCServiceAcceptItem);
    getAcceptItem().add(rCServiceAcceptItem);
    return rCServiceAcceptItem;
    
  }
  
  public RCServiceAcceptItem createAndAddtoAcceptItem(final Procedure1<RCServiceAcceptItem> updater) {
    getAcceptItem().size();
    com.shrct.businessprocess.purchase.RCServiceAcceptItem rCServiceAcceptItem = new com.shrct.businessprocess.purchase.impl.RCServiceAcceptItemImpl(this);
    getObjectFactory().create(rCServiceAcceptItem);
    getAcceptItem().add(rCServiceAcceptItem);
    return rCServiceAcceptItem;
    
  }
  
  public RCServicePurchaseOrderAccept removeAllAcceptItem() {
    for(RCServiceAcceptItem obj : this.acceptItem){
    	getObjectFactory().delete(obj);
    }
    this.acceptItem.clear();
    return this;	
    
  }
  
  public RCServicePurchaseOrderAccept removeFromAcceptItem(final RCServiceAcceptItem rCServiceAcceptItem) {
    this.acceptItem.remove(rCServiceAcceptItem);
    getObjectFactory().delete(rCServiceAcceptItem);
    return this;
    
  }
  
  /**
   * 到货总金额
   */
  public RCProjectMoney getTotalAmount() {
    final Function1<RCServiceAcceptItem,RCProjectMoney> _function = new Function1<RCServiceAcceptItem,RCProjectMoney>() {
        public RCProjectMoney apply(final RCServiceAcceptItem it) {
          RCProjectMoney _price = it.getPrice();
          return _price;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCServiceAcceptItem, RCProjectMoney>map(this.acceptItem, _function);
    RCExchangeRate _defaultExchangeRate = this.purchaseOrder.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
    return _sum;
  }
  
  public void acceptProductService(final String name, final String serviceSpec, final String serviceContractCode, final RCSupplier productSupplier, final RCIdentifiablePhysicalProductEntry product, final RCServiceLevel serviceLevel, final RCServiceResponseTime serviceResponseTime, final Date startDate, final Date dueDate, final RCProjectMoney price, final Boolean forPartDevice) {
    BeanFactory _beanFactory = this.getBeanFactory();
    RCProductServiceTracerDAO _bean = _beanFactory.<RCProductServiceTracerDAO>getBean(RCProductServiceTracerDAO.class);
    RCServiceProductPurchaseRequest _purchaseRequest = this.purchaseOrder.getPurchaseRequest();
    IHasServicePurchase _serviceBudget = _purchaseRequest.getServiceBudget();
    RCFullServiceProductEntry _acceptProductService = _bean.acceptProductService(_serviceBudget, name, serviceSpec, serviceContractCode, productSupplier, product, serviceLevel, serviceResponseTime, startDate, dueDate, price, forPartDevice);
    final Procedure1<RCFullServiceProductEntry> _function = new Procedure1<RCFullServiceProductEntry>() {
        public void apply(final RCFullServiceProductEntry entry) {
          RCServiceAcceptItem _createAndAddtoAcceptItem = RCServicePurchaseOrderAcceptImpl.this.createAndAddtoAcceptItem();
          final Procedure1<RCServiceAcceptItem> _function = new Procedure1<RCServiceAcceptItem>() {
              public void apply(final RCServiceAcceptItem it) {
                it.setProductEntry(entry);
                it.setPrice(price);
              }
            };
          ObjectExtensions.<RCServiceAcceptItem>operator_doubleArrow(_createAndAddtoAcceptItem, _function);
        }
      };
    ObjectExtensions.<RCFullServiceProductEntry>operator_doubleArrow(_acceptProductService, _function);
  }
  
  public static RCServicePurchaseOrderAccept create(final RCServicePurchaseOrder purchaseOrder, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCServicePurchaseOrderAccept rCServicePurchaseOrderAccept = new com.shrct.businessprocess.purchase.impl.RCServicePurchaseOrderAcceptImpl(
    	purchaseOrder
    );
    objectFactory.create(rCServicePurchaseOrderAccept);
    return rCServicePurchaseOrderAccept;			
    
  }
  
  public static RCServicePurchaseOrderAccept createRCServicePurchaseOrderAcceptImpl(final RCServicePurchaseOrder purchaseOrder, final IObjectFactory objectFactory, final Procedure1<RCServicePurchaseOrderAccept> init) {
    com.shrct.businessprocess.purchase.RCServicePurchaseOrderAccept rCServicePurchaseOrderAccept = new com.shrct.businessprocess.purchase.impl.RCServicePurchaseOrderAcceptImpl(
    	purchaseOrder
    );
    objectFactory.create(rCServicePurchaseOrderAccept);
    init.apply(rCServicePurchaseOrderAccept);
    
    return rCServicePurchaseOrderAccept;			
    
  }
  
  public RCServicePurchaseOrderAcceptProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCServicePurchaseOrderAcceptProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCServicePurchaseOrderAcceptProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
